package com.linko.intelligent.common.util;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONLexer;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class CustomDateDeserializer implements ObjectDeserializer {

    private SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US);

    @Override
    public Date deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
        JSONLexer lexer = parser.lexer;
        try {
            String dateString = lexer.stringVal();
            return dateFormat.parse(dateString);
        } catch (ParseException e) {
            throw new JSONException("Error parsing date string", e);
        }
    }

    @Override
    public int getFastMatchToken() {
        return 0;
    }
}

